package com.jiyun.utils;


import com.jiyun.model.Access;
import com.jiyun.model.Catalog;

import java.util.ArrayList;
import java.util.List;

/* *
 * @Author nxj
 * @Date 2023 08 2023/8/5 11 45
 * */
public class ThreeBuild {
    public List<Catalog> catalogList=new ArrayList<>();



    public ThreeBuild(List<Catalog> catalogList){
        this.catalogList=catalogList;
    }

    //所有根节点
    public List<Catalog> getCatalog(){
        List<Catalog> rootCatalogList = new ArrayList<>();
        for (Catalog catalog : catalogList) {
            if(0==catalog.getPid()){
                rootCatalogList.add(catalog);
            }
        }
        return rootCatalogList;
    }
    //根据根节点构造树
    public List<Catalog> buildCatalog(){
        List<Catalog> treeCatalog=new ArrayList<>();
        for (Catalog treeRootCatalog : getCatalog()) {
            treeRootCatalog=buildChildCatalog(treeRootCatalog);
            treeCatalog.add(treeRootCatalog);
        }
        return treeCatalog;
    }

    //创建子树结构
    public Catalog buildChildCatalog(Catalog pNode){
        List<Catalog> childCatalog=new ArrayList<>();
        for (Catalog catalog1 : catalogList) {
            if(catalog1.getPid().equals(pNode.getId())){
                childCatalog.add(buildChildCatalog(catalog1));
            }
        }
        pNode.setChildren(childCatalog);
        return pNode;
    }

}
